// pages/user/renzheng.js
import ajax from "../../../utils/ajax.js";
let QQMapWX = require("../../../utils/qqmap-wx-jssdk.min.js");
var qqmapsdk;
let app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    citys: [
    ],
    init_city: [0, 0, 0],
    city_text: '请选择所在地区',
    province_id: '',
    city_id: '',
    district_id: '',
    img_url: "",
    qiyexingzhi: [],
    qiyexingzhi_id: '',
    qiyexingzhi_text: '请选择企业性质',
    qiyeguimo:[],
    qiyeguimo_id: "",
    qiyeguimo_text: "请选择企业规模",
    suoshuhangye:[],
    suoshuhangye_id:'',
    suoshuhangye_text:'请选择所属行业',
    logo_src:'',
    show_select_fl:false,//是否显示选择福利
    fl_list:[],//福利数据列表
    fl_text:'请选择企业福利',
    animate:'',
    citys:'',
    init_address_detail:'',


  },
    // 选择地址
    select_address(){
      let that = this;
      wx.chooseLocation({
        success(res){
          // console.log(res);
          let lng = res.longitude+"";
          let lat = res.latitude+"";
          qqmapsdk.reverseGeocoder({
            location:lat+","+lng,
            coord_type:5,
            success(map_res){
              console.log(map_res.result)
              that.setData({
                wx_select_address_info:map_res.result,
                city_text:map_res.result.address_component.province+" "+map_res.result.address_component.city+" "+map_res.result.address_component.district,
                init_address_detail:map_res.result.formatted_addresses.recommend,
                work_address_detail:map_res.result.formatted_addresses.recommend
              });
              let province = map_res.result.address_component.province;
              let city = map_res.result.address_component.city;
              let district = map_res.result.address_component.district;
              that.format_area({
                province,city,district
              })
            }
          })
        }
      })
    },
    // 省市区文字  转化为ID
    format_area(area){
      console.log(area)
      let province = area.province.replace("省","").replace("市","")
      let city = area.city.replace("省","").replace("市","")
      let district = area.district.replace("省","").replace("市","")
      let province_id="";
      let city_id="";
      let district_id="";
      let citys = this.data.citys
      for(let i = 0; i<citys.length;i++){
        if(citys[i].name==province){
          province_id = citys[i].id;
          let city2 = citys[i].city;
  
          for(let j=0;j<city2.length;j++){
            if(city2[j].name==city){
              city_id = city2[j].id;
              let city3 = city2[j].district;
  
              for(let k=0;k<city3.length;k++){
                if(city3[k].name==district){
                  district_id = city3[k].id
                  break
                }
                
              }
              break
            }
            
          }
          break
        }
       
        
      }
      console.log(province_id+","+city_id+","+district_id);
      this.setData({province_id,city_id,district_id})
    },
  // 提交
  formSubmit(e) {
    console.log(this.data.fl_ids.toString());
 
    let form_data = e.detail.value;
    if (!form_data.qiyemingcheng) {app.toast("请输入企业名称");return}
    // if (!form_data.qiyejiancheng) {app.toast("请输入企业简称");return}
    if (!this.data.qiyexingzhi_id) {app.toast("请选择企业性质");return}
    if (!this.data.qiyeguimo_id) {app.toast("请选择企业规模");return}
    if (!this.data.suoshuhangye_id) {app.toast("请选择所属行业");return}
    if (!form_data.qiyejianjie) {app.toast("请输入企业简介");return}
    // if (!this.data.fuli_id) {app.toast("请选择企业福利");return}



    if (!form_data.lianxiren) {app.toast("请输入联系人");return}
    if (!form_data.shoujihao) {app.toast("请输入联系电话");return}
    if (!form_data.youxiang) {app.toast("请输入邮箱地址");return}



    if (!form_data.qiyejianjie) {app.toast("请输入企业简介");return}
    if (!this.data.province_id) {app.toast("请选择地区");return}
    if (!form_data.address) {app.toast("请输入详细地址");return}

    // if (!form_data.legal_person_name) {app.toast("请输入法人名称");return}
    // if (!form_data.legal_person_id_card) {app.toast("请输入身份证号");return}
    if (!this.data.img_url) {app.toast("请上传营业执照"); return}
    if (!this.data.logo_src) {app.toast("请上传logo图片"); return}

    let data = {
      // province: this.data.province_id,//省ID
      // city: this.data.city_id,//市ID
      district:this.data.province_id+","+ this.data.city_id+","+ this.data.district_id,//省市区 ID  逗号隔开
      name: form_data.qiyemingcheng,//企业名称
      // short_name:form_data.qiyejiancheng,//企业简称
      nature:this.data.qiyexingzhi_id,//企业性质
      trade:this.data.suoshuhangye_id,//行业
      scale:this.data.qiyeguimo_id,//规模
      intro:form_data.qiyejianjie,//简介
      describe:'',//介绍
      welfare:this.data.fl_ids.toString(),//福利
      contact:form_data.lianxiren,//联系人
      mobile:form_data.shoujihao,//手机号
      email:form_data.youxiang,//邮箱
      // legal_person_name: form_data.legal_person_name,//法人姓名
      // legal_person_id_card: form_data.legal_person_id_card,//法人身份证号码
      image: this.data.img_url,//营业执照
      logo:this.data.logo_src,//logo
      address:form_data.address,//详细地址
    }

    console.log(data)
   
    ajax.post({
      url: 'user/auth',
      data
    }).then(res => {
      if (res.code == 0) {
        app.toast("提交成功");
        
        // let pages = getCurrentPages();
        // let prevPage = pages[pages.length - 3];
        app.common.user_info.auth_status = 1;
        let user_info =wx.getStorageInfoSync("user_info");
        user_info.is_auth = 1;
        wx.setStorageSync('user_info', user_info)
        // prevPage.update_user_info()
        setTimeout(() => {
          wx.navigateBack({
            delta:2
          })
        }, 600);
      } else {
        app.toast(res.message)
      }
    })
  },
  // 上传logo
  upload_logo(){
    ajax.upload_img().then(res => {
      
      this.setData({ logo_src: res[0] })
    })
  },
  // 更换省市区 更新子级数据
  change_city_col(e) {
    let citys = this.data.citys;
    console.log(e.detail)
    // 更换省
    if (e.detail.column == 0) {
      // 重置市 的数据
      citys[1] = citys[0][e.detail.value].city;
      // 重置区 的数据
      citys[2] = citys[0][e.detail.value].city[0].district;
      this.setData({
        citys: citys,
        // 将市区改为当前第一项
        init_city: [e.detail.value, 0, 0]
      })
    }
    // 更换市
    if (e.detail.column == 1) {
      // 市级数据
      let city2 = this.data.citys[1];
      // 市级选中的下标
      let city2_index = e.detail.value;
      // 改变区级数据
      citys[2] = city2[city2_index].district;
      let init_city = this.data.init_city;
      init_city[1] = e.detail.value;
      init_city[2] = 0;
      this.setData({
        citys: citys,
        init_city
      })
    }
  },
  // 省市区ID
  picker_on_change(e) {
    console.log(e.detail.value);
    let value_text = this.data.citys[0][e.detail.value[0]].name + " " +
      this.data.citys[1][e.detail.value[1]].name + " " +
      this.data.citys[2][e.detail.value[2]].name;

    let province_id = this.data.citys[0][e.detail.value[0]].id;
    let city_id = this.data.citys[1][e.detail.value[1]].id;
    let district_id = this.data.citys[2][e.detail.value[2]].id;
    this.setData({ city_text: value_text, province_id, city_id, district_id })
    console.log(value_text)
  },
  // 初始化地区选择数据
  init_picker_data(data) {
    let city1 = data;
    let city2 = data[0].city;
    let city3 = data[0].city[0].district;
    this.setData({
      citys: [city1, city2, city3]
    })
  },
  // 上传营业执照
  upload_img() {
    ajax.upload_img().then(res => {
      console.log(res);
      this.setData({ img_url: res[0] })
    })
  },
  // 初始化 企业 性质/规模/行业 数据
  init_company_picker_data() {
    // 企业性质
    ajax.get({
      url: 'fulltime/nature'
    }).then(res => {
      console.log(res);
      this.setData({
        qiyexingzhi: res.data.list_data
      })
    })
    // 企业规模
    ajax.get({
      url: 'fulltime/scale'
    }).then(res => {
      console.log(res);
      this.setData({
        qiyeguimo: res.data.list_data
      })
    })
    // 所属行业
    ajax.get({
      url: 'fulltime/trade'
    }).then(res => {
      console.log(res);
      this.setData({
        suoshuhangye: res.data.list_data
      })
    })
    // 企业福利

    ajax.get({
      url: 'fulltime/welfare'
    }).then(res => {
      console.log(res);
      this.setData({
        qiyefuli: res.data.list_data
      })
    })
  },
  // 企业  性质/规模/行业 选择
  company_picker_on_change(e) {
    let type = e.currentTarget.dataset.type;
    let item;
    switch (type) {
      case 'qiyexingzhi':
        item = this.data.qiyexingzhi[e.detail.value];
        console.log(item)
        this.setData({
          qiyexingzhi_id: item.id,
          qiyexingzhi_text: item.value
        })
        break;
      case 'qiyeguimo':
        item = this.data.qiyeguimo[e.detail.value];
        console.log(item)
        this.setData({
          qiyeguimo_id: item.id,
          qiyeguimo_text: item.value
        })
        break;
      case 'suoshuhangye':
        item = this.data.suoshuhangye[e.detail.value];
        console.log(item)
        this.setData({
          suoshuhangye_id: item.id,
          suoshuhangye_text: item.value
        })
        break;

      default:
        break;
    }

  },
  // 选择 企业福利
  select_this_fl(e){
    console.log(e.currentTarget.dataset);
    let fl_list = this.data.fl_list;
    fl_list[e.currentTarget.dataset.index].selected = !fl_list[e.currentTarget.dataset.index].selected
    
    let fl_ids = [];
    let fl_text = [];
    for(let i=0;i<fl_list.length;i++){
      if(fl_list[i].selected){
        fl_ids.push(fl_list[i].id);
        fl_text.push(fl_list[i].value)
      }
    }
    this.setData({
      fl_list,
      fl_ids,
      fl_text:fl_text.toString()
    });
  },
  // 选择企业福利 开关
  switch_select_fl(){
    console.log("switch_select_fl")
    let animate = wx.createAnimation({
      timingFunction:'ease-in',
      duration:200
    })
   
    
    if(this.data.show_select_fl){
      // 关
      animate.opacity(0).step();

      setTimeout(()=>{
        this.setData({
          show_select_fl:false
        })
      },400)
      
    }else{
      // 开
      this.setData({
        show_select_fl:true
      })
      animate.opacity(1).translateY(0).step();
    }
    this.setData({
      animate:animate.export()
    })
  
    
  },
  // 获取 企业福利  数据
  get_company_fl_data(){
    ajax.get({
      url:'fulltime/welfare',
    }).then(res=>{
      console.log(res)
      for(let i=0;i<res.data.list_data.length;i++){
        res.data.list_data.selected=false;
      }
      this.setData({
        fl_list:res.data.list_data
      })
    })
  },
  onLoad: function (options) {
    qqmapsdk = new QQMapWX({
      key: 'E7LBZ-2GLK5-P4SIG-QXJPX-CFN7H-JUBMB'
    });
    // 获取省市二级菜单数据
    ajax.get({
      url: 'lbs/province_city_list',
    }).then(res => {
      if (res.code == 0) {
        let data = res.data.list_date;
        this.setData({
          citys:data
        })
        // this.init_picker_data(data)
      } else {
        app.toast(res.msg)
      }
    })
    this.init_company_picker_data();
    this.get_company_fl_data()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})